package cn.nfwx.tv.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by  zhouzechao on 2017/11/17 0018.
 */

public class TimeUtil {
    public final static long MINUTE = 60 * 1000;// 1分钟
    public final static long HOUR = 60 * MINUTE;// 1小时
    public final static long DAY = 24 * HOUR;// 1天

    /**
     * @param time
     * @return
     */
    public static String getFriendlyTime(long time) {

        Date date = new Date(time);

        if (date == null) {
            return "";
        }
        long diff = new Date().getTime() - date.getTime() * 1000;
        long r = 0;
        if (diff > DAY) {
            r = (diff / DAY);
            return r + "天前";
        }
        if (diff > HOUR) {
            r = (diff / HOUR);
            return r + "小时前";
        }
        if (diff > MINUTE) {
            r = (diff / MINUTE);
            return r + "分钟前";
        }
        return "刚刚";
    }

    public static int getDateCount(long time) {
        Date date = new Date(time);
        if (date == null) {
            return 0;
        }
        long diff = new Date().getTime() - date.getTime() * 1000;
        long r = 0;
        if (diff > DAY) {
            r = (diff / DAY);
            return (int) r;
        }
        return 1;
    }

    public static Date timeToDate(long time) {
        Date date = new Date(time * 1000);
        return date;
    }

    /**
     * 根据时分秒获取总秒数
     *
     * @param date
     * @return
     */
    public static long getTimeSeconds(Date date) {
        return date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds();
    }

    /**
     * @param time
     * @return
     */
    public static String getFormatTime(long time) {

        Date date = new Date(time * 1000);

        if (date == null) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeStr = format.format(date);
        return timeStr;
    }

    /**
     * @param time
     * @return
     */
    public static String getFormat2Time(long time) {

        Date date = new Date(time * 1000);

        if (date == null) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String timeStr = format.format(date);
        return timeStr;
    }

    /**
     * @param dateStr
     * @return
     */
    public static String getFormat2Time(String dateStr) {
        if (dateStr == null) {
            return "";
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        Date time;
        try {
            dateStr = dateStr.replace("Z", " UTC");
            time = df.parse(dateStr);
            return getFormat2Time(time.getTime() / 1000L);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * @param dateStr
     * @return
     */
    public static Date getFormat2DateHm(String dateStr) {
        if (dateStr == null) {
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        Date time;
        try {
            time = df.parse(dateStr);
            return time;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param dateStr
     * @return
     */
    public static Date getFormat2DateYYMMDDHHmm(String dateStr) {
        if (dateStr == null) {
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date time;
        try {
            time = df.parse(dateStr);
            return time;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 毫秒转时间
     *
     * @param time
     * @return
     */
    public static String toTime(int time) {
        time /= 1000;
        int minute = time / 60;
        int hour = minute / 60;
        int second = time % 60;
        minute %= 60;
        return String.format("%02d:%02d:%02d", hour, minute, second);
    }


    public static String getFormatYYMMddTime(Date date) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = format.format(date);
        return timeStr;
    }

    /**
     * @param time
     * @return
     */
    public static String getFormatMMddTime(long time) {

        Date date = new Date(time * 1000);

        if (date == null) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("MM/dd");
        String timeStr = format.format(date);
        return timeStr;
    }

    /**
     * @param time
     * @return
     */
    public static String getFormatYYMMddTime(long time) {

        Date date = new Date(time * 1000);

        if (date == null) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String timeStr = format.format(date);
        return timeStr;
    }

    public static String getFormatHHmmDate(Date time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        String timeStr = format.format(time);
        return timeStr;
    }


    public static String getFormatMMddDate(Date time) {

        SimpleDateFormat format = new SimpleDateFormat("MM/dd");
        String timeStr = format.format(time);
        return timeStr;
    }

    public static String getFormatHHmmssDate(long time) {
        Date date = new Date(time * 1000);
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        String timeStr = format.format(date);
        return timeStr;
    }


    /**
     * @param time
     * @return
     */
    public static String getTodayTime(long time) {

        Date date = new Date(time * 1000);

        if (date == null) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        String timeStr = format.format(date);
        return timeStr;
    }

    //判断选择的日期是否是本周
    public static boolean isThisWeek(long time) {
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(new Date(time * 1000));
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        if (paramWeek == currentWeek) {
            return true;
        }
        return false;
    }

    //判断选择的日期是否是今天
    public static boolean isToday(long time) {
        return isThisTime(time * 1000, "yyyy-MM-dd");
    }

    public static boolean isThisTime(long time, String pattern) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String param = sdf.format(date);//参数时间
        String now = sdf.format(new Date());//当前时间
        if (param.equals(now)) {
            return true;
        }
        return false;
    }

    public static boolean isThisTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String param = sdf.format(date);//参数时间
        String now = sdf.format(new Date());//当前时间
        if (param.equals(now)) {
            return true;
        }
        return false;
    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @param time
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(long time) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(time * 1000));
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * 返回时分
     *
     * @param time
     * @return
     */
    public static String getCallLiveTime(long time) {

        int hours = (int) (time / 3600);
        int minute = (int) ((time - hours * 3600) / 60);
        return hours + ":" + minute;
    }

    /**
     * * 返回当前时间年月日
     *
     * @return
     */
    public static String getToday() {
        Date date = new Date();
        if (date == null) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = format.format(date);
        return timeStr;
    }

    /**
     * 获取当前时间前几天或者后几天的时间
     *
     * @param preday
     * @return
     */
    public static String getTodayMMdd(int preday) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, preday);
        date = calendar.getTime();
        return sdf.format(date);
    }

    /**
     * 获取当前时间前几天或者后几天的时间
     *
     * @param preday
     * @return
     */
    public static String getTodayYYMMdd(int preday) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, preday);
        date = calendar.getTime();
        return sdf.format(date);
    }

    /**
     * 设置
     *
     * @return
     */
    public static long getTime() {
        return System.currentTimeMillis() / 1000L;
    }

    public static boolean getDateMoreThan(String str1) {
        try {
            DateFormat fmtDateTime = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = fmtDateTime.parse(str1.toString());
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(date1);
            Calendar today = Calendar.getInstance();
            double days = (today.getTimeInMillis() - calendar1.getTimeInMillis()) / (1000 * 60 * 60 * 24);
            if (days > 7) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
